// 公告
layui.define(["table", "form", "upload", "admin"], function (exports) {
    var $ = layui.$,
        table = layui.table,
        form = layui.form,
        upload = layui.upload,
        admin = layui.admin;

    table.render({
        elem: "#LAY-news-manage",
        url: "/admin/news/getlist",
        cols: [
            [
                {field: "id", width: 80, title: "ID", sort: true},
                {field: "author_name", title: "小编名称", width: 120},
                {
                    field: '', width: 100, title: '分类', templet: function (d) {
                        return d.category ? d.category.category_name : '';
                    }
                },
                {field: "rand_hits", title: "阅读量", width: 110},
                {field: "title", title: "标题", width: 350},
                {field: "content", title: "内容", minWidth: 200},
                {
                    field: '', width: 100, title: '状态', templet: function (d) {
                        return d.status == 1 ? '<span style="color:#009688">上架中</span>' : '<span style="color:#FF5722">已下架</span>';
                    }
                },
                {
                    field: '', width: 80, title: '热门', templet: function (d) {
                        return d.is_hot == 1 ? '<span style="color:#009688">是</span>' : '<span style="color:#FF5722">否</span>';
                    }
                },
                {
                    field: '', width: 100, title: '是否轮播图', templet: function (d) {
                        return d.is_banner == 1 ? '<span style="color:#009688">是</span>' : '<span style="color:#FF5722">否</span>';
                    }
                },
                {field: "publish_time", title: "时间", width: 180},
                {
                    title: "操作",
                    width: 180,
                    align: "center",
                    fixed: "right",
                    toolbar: "#table-notice",
                },
            ],
        ],
        page: true,
        limit: 20,
        height: "full-140",
        text: "对不起，加载出现异常！",
        even: true,
    });

    //监听工具条
    table.on("tool(LAY-news-manage)", function (obj) {
        if (obj.event === "edit") {
            var index = layer.open({
                type: 2,
                title: "编辑资讯",
                content: "/admin/news/edit?id=" + obj.data.id,
                maxmin: true,
                area: ["650px", "500px"],
            });
            layer.full(index);
        } else if (obj.event === "del") {
            layer.confirm("确定要删除该资讯吗？", function (index) {
                layer.load();
                admin.req({
                    url: "/admin/news/del_post",
                    data: {id: obj.data.id},
                    success(res) {
                        layer.closeAll("loading");
                        if (res.code == 0) {
                            table.reload("LAY-news-manage");
                            layer.msg(res.msg, {icon: 1});
                        }
                    },
                    complete: function () {
                        layer.closeAll("loading");
                    },
                });
            });
        }
    });


    //上传图片
    upload.render({
        url: '/admin/index/noOperation'
        , elem: '.LAY_iconImgUpload'
        , auto: true
        , choose: function (obj) {
            // 图片地址表单id
            var imginput_obj_id = this.idObjName;

            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: '/admin/activity/signForCos'
                , success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime
                                });
                            }
                        });
                        cos.putObject({
                            Bucket: res.data.bucket,
                            Region: res.data.region,
                            Key: 'images/' + res.data.filename + choosefile.name,
                            Body: choosefile,
                        }, function (err, data) {
                            layer.closeAll('loading');
                            if (err) {
                                console.log(err);
                                layer.msg("上传失败", {icon: 5});
                            } else {
                                if (data.statusCode == 200) {
                                    $('#' + imginput_obj_id).val("https://" + data.Location);
                                } else {
                                    layer.msg("图片上传失败", {icon: 5});
                                }
                            }
                        });
                    } else {
                        layer.closeAll('loading');
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            })
        }
    });

    //查看图标
    admin.events.coverImgPreview = function (othis) {
        var pic_obj_id = othis.data('idname');
        var src = $('#' + pic_obj_id).val();
        layer.photos({
            photos: {
                "title": "查看图标" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    //列表查看
    admin.events.photosPreview = function (othis) {
        let urls = othis.attr("value");
        let urlarr = new Array();
        $.each(urls.split(","), function (i, val) {
            urlarr.push({"src": val});
        });
        layer.photos({
            photos: {
                "title": "查看照片" //相册标题
                , "data": urlarr
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };


    function showBannerDesc() {
       var  banner_check = $('#isBannerArea input[name=is_banner]:checked').val();
        if (banner_check == 1) {
            $('#imgDesc').text('轮播图图片大小：1013*366');
        } else {
            $('#imgDesc').text(' 封面图大小：200*120');
        }
    }

    showBannerDesc();

    //此处即为 radio 的监听事件
    form.on('radio(is_banner)', function (data) {
        showBannerDesc();
    });

    exports("news", {});
});
